package com.ruoyi.web.controller.push;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.domain.vo.PreventionPushVO;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * 企微推送
 */
//@RestController
//@RequestMapping("/preventionPush")
public class PreventionPushController extends BaseController {

    // 域名
    @Value("${qywx.url}")
    private String url;
    // id
    @Value("${qywx.corpid}")
    private String corpid;
    // token
    @Value("${qywx.corpsecret}")
    private String corpsecret;
    // send
    @Value("${qywx.sendUrl}")
    private String sendUrl;

    @Autowired
    private RedisCache redisCache;


    /**
     * 获取token信息
     * @return
     */
    @GetMapping("/getQywxToken")
    public JSONObject getQywxToken() {
        String strUrl = url + corpid + corpsecret;
        String token = HttpUtils.sendGet(strUrl);
        JSONObject jsonObject = JSONObject.parseObject(token);
        String errcode = jsonObject.get("errcode").toString();
        String errmsg = jsonObject.get("errmsg").toString();
        String expires_in = jsonObject.get("expires_in").toString();
        String access_token = jsonObject.get("access_token").toString();
        redisCache.setCacheObject("token", access_token, 7300, TimeUnit.SECONDS);
        return jsonObject;
    }




    /**
     * 发过消息
     */
    @GetMapping("/sendInfo")
    public String sendInfo() {
        // 查询缓存是否有token信息
        PreventionPushVO pushVO = redisCache.getCacheObject("token");
        if (ObjectUtils.isNotEmpty(pushVO)){
            // 如果缓存有Key则取
            if (StringUtils.isNotEmpty(pushVO.getAccessToken())){
                String strSendUrl = sendUrl + pushVO.getAccessToken();
                String sendPost = HttpUtils.sendPost(sendUrl + pushVO.getAccessToken(), pushVO.getAccessToken());
                System.out.println("sendPost：" + sendPost);
            }
        }else {
            // 否者 请求第三方接口 获取token
            String token = HttpUtils.sendGet(url + corpid + corpsecret);
            JSONObject jsonObject = JSONObject.parseObject(token);
            String sendPost = HttpUtils.sendPost(sendUrl + jsonObject.get("access_token").toString(), jsonObject.get("access_token").toString());
            System.out.println("sendPost：" + sendPost);
        }
        return "";
    }



















    public static void main(String[] args) {
        System.out.println("36位-UUID:");
        System.out.println(UUID.randomUUID().toString());
        System.out.println(UUID.randomUUID().toString());
        System.out.println(UUID.randomUUID().toString());
        System.out.println(UUID.randomUUID().toString());

/* String strUrl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ww8761b6451cbb9e20&corpsecret=V7c_iiMTv8GDRW7AQDIBVj9eW3K-zdgiVaZeqrM_vGA";
        String token = HttpUtils.sendGet(strUrl);
        System.out.println("token: " + token);*/
/*
        String token ="{\"errcode\":0,\"errmsg\":\"ok\",\"access_token\":\"-k5CqMaqXxRJLg_qHMbB0yhSvjmhzQGuCOSZPkZ8CXirPW6CQgp5SwuoclUeI5w9En4KrtJvPOePKJAUUa7iwoATIMZPj8oTuMo5RSMmt4y-WdEchxdQD3uJ6axxdyelJFIwdhPKejygp8OXSvLIEIBTl09cYyAUrXVIw08DymgZSZYmb68aKv9L0sIP6Z8SoOJTAz7xKnQsiyfj26lqYQ\",\"expires_in\":7200}";
        JSONObject jsonObject = JSONObject.parseObject(token);
        String errcode = jsonObject.get("errcode").toString();
        String errmsg = jsonObject.get("errmsg").toString();
        String expires_in = jsonObject.get("expires_in").toString();
        String access_token = jsonObject.get("access_token").toString();
        redisCache.setCacheObject("token", access_token, 7300, TimeUnit.SECONDS);
        redisCache.getCacheObject(token);
        System.out.println(access_token);*/
       /* String url ="https://qyapi.weixin.qq.com/cgi-bin/gettoken?";
        String corpid = "ww8761b6451cbb9e20";
        String corpsecret = "V7c_iiMTv8GDRW7AQDIBVj9eW3K-zdgiVaZeqrM_vGA";*/



//        String urlTwo ="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=";
//        String token = "{\n" +
//                "    \"errcode\":0,\n" +
//                "    \"errmsg\":\"ok\",\n" +
//                "    \"access_token\":\"Ejyqd6DsulS6LF_wVxb1Z14kFx-gpKBA57GklFk73P1MjHcwaN9qFzk69BgDFlz4ew5NDUz_VryncG174V6zpcfxopsTNCZ4ZJ0WPcx_3ipINkHFXWV91Ll7rZmAODKeWs4fZM_7QWvAxyb6zDzCmo5YrJqLRUnj9ttT_3jax0r96dpeuhUqjpU7ClE7h_dU5wX3TBiBHccsr2RUxK86zA\",\n" +
//                "    \"expires_in\":7200\n" +
//                "}";
//        JSONObject jsonObject = JSONObject.parseObject(token);
//
//        String accessToken = jsonObject.get("access_token").toString();
//        String sendPost = HttpUtils.sendPost(urlTwo + accessToken, null);
//        System.out.println("sendPost：" + sendPost);
    }

}

